package controllers.power.admin;

import controllers.BaseController;
import controllers.auth.Secure;
import helper.GlobalConfig;
import models.admin.AdminRole;
import models.admin.AdminUser;
import models.admin.AdminUserRole;
import models.constants.AvailableStatus;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import org.apache.commons.lang.StringUtils;
import play.modules.paginate.JPAExtPaginator;
import play.mvc.Controller;
import play.mvc.With;
import util.common.ConvertUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by youliangcheng on 17/4/8.
 */
@With(BaseController.class)
public class AdminUserCtrl extends Controller {
    public static void loadUserList(Integer pageNumber,
                                    Long id,
                                    String name,
                                    String loginName,
                                    String phoneNo,
                                    Long merchantId) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        Map<String, Object> searchMap = new HashMap<>();
        if (id != null)
            searchMap.put("id", id);

        if (StringUtils.isNotBlank(name))
            searchMap.put("name", "%" + name + "%");

        if (StringUtils.isNotBlank(loginName))
            searchMap.put("loginName", "%" + loginName + "%");

        if (StringUtils.isNotBlank(phoneNo))
            searchMap.put("phoneNo", "%" + phoneNo + "%");

        if (merchantId != null && merchantId > 0) {
            Merchant merchant = Merchant.findByMerchantId(merchantId);
            searchMap.put("merchant", merchant);
        }

        List<Merchant> merchantList = Merchant.loadAllMerchat();

        JPAExtPaginator<AdminUser> resultPage = AdminUser.findByCondition(searchMap, "id desc", pageNumber, GlobalConfig.PAGE_SIZE);
        render(resultPage, pageNumber, merchantList, name, loginName, phoneNo, merchantId);
    }

    //删除
    public static void deletedAdminUser(long id, Integer pageNumber) {
        AdminUser adminUser = AdminUser.findAdminUserById(id);
        adminUser.deleted = DeletedStatus.DELETED;
        adminUser.save();
        flash.put("msg", "删除成功");
        redirect("/power/admin/AdminUserCtrl/loadUserList/" + pageNumber);

    }

    //加载数据
    public static void loadUserDetail(long id, Integer pageNumber) {
        List<Merchant> merchantList = Merchant.loadAllMerchat();
        List<AdminRole> roleList = AdminRole.loadRoleList();
        AdminUser adminUser = new AdminUser();
        AdminUserRole userRole = new AdminUserRole();
        if (id > 0) {
            adminUser = AdminUser.findAdminUserById(id);
            userRole = AdminUserRole.findUserById(adminUser.id);
        }
        render(id ,adminUser, pageNumber, merchantList, roleList, userRole);

    }

    //保存
    public static void saveAdminUser(AdminUser adminUser, Integer pageNumber, long roleId) {
        AdminRole adminRole = null;
        if (roleId > 0)
            adminRole = AdminRole.findById(roleId);

        //update
        if (adminUser != null && adminUser.id != null) {
            adminUser.modifyAt = new Date();
            adminUser.update();

        } else {//new
            //判断用户登录名是否存在
            AdminUser exitUser = AdminUser.findByLoginName(adminUser.loginName);
            if(exitUser != null){
                flash.put("msg", "登录用户名已经存在,保存失败!");
                redirect("/power/admin/AdminUserCtrl/loadUserList/" + pageNumber);
            }
            adminUser.ctUserId = Secure.getLoginUserId();//创建人Id
            adminUser.deleted = DeletedStatus.UN_DELETED;
            adminUser.availableStatus = AvailableStatus.AVAILABLE;//启用状态
            adminUser.createAt = new Date();
            adminUser.password = ConvertUtil.toMD5("123456");//默认密码
            adminUser.save();
        }

        //新建或更新 adminUserOperate
        AdminUserRole adminUserRole = AdminUserRole.findUserById(adminUser.id);
        if (adminUserRole == null && adminRole != null) { //new
            adminUserRole = new AdminUserRole();
            adminUserRole.adminRole = adminRole;
            adminUserRole.adminUser = adminUser;
            Secure.setBaseInfo(adminUserRole);
            adminUserRole.save();
        } else if (adminUserRole != null && adminUserRole.adminRole.id != roleId) { //update
            adminUserRole.adminRole = AdminRole.findById(roleId);
            Secure.setBaseInfo(adminUserRole);
            adminUserRole.save();

        }

        flash.put("msg", "保存成功");
        redirect("/power/admin/AdminUserCtrl/loadUserDetail/" + adminUser.id + "/" + pageNumber);

    }


}
